package com.example.doer.reading;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 严旭江 on 2017/7/14.
 */

//回复Topic的TopicReply  对应的listView的适配器
public class TopicReplyListAdapter extends BaseAdapter {
    List<TopicReply>topicReplyList;
    Context context;
    int ResID;

    private PopupWindow myPopupWindow;
    private int myPopupWindowWidth;
    private int myPopupWindowHeight;

    public TopicReplyListAdapter(Context theContext,List<TopicReply>theList,int theResID){
        //context赋值 为一个TopicWithReplyActivity
        this.context = theContext;
        this.topicReplyList = theList;
        this.ResID = theResID;
    }

    //实现适配器
    @Override
    public int getCount(){
        return topicReplyList.size();
    }

    public TopicReply getItem(int position){
        return topicReplyList.get(position);
    }
    public long getItemId(int position){
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent){
        convertView = LayoutInflater.from(context).inflate(ResID,null);

        final TopicReply topicReply = topicReplyList.get(position);

        //Reply 的用户头像
        ImageView userImageView = (ImageView)convertView.findViewById(R.id.userResOfTopicReplyListView);
        TextView userName = (TextView)convertView.findViewById(R.id.userNameOfTopicReplyListView);
        TextView replyContent = (TextView)convertView.findViewById(R.id.replyContentOfTopicReplyListView);
        TextView replyFavourCount = (TextView)convertView.findViewById(R.id.favourCountOfTopicReplyListView);
        TextView replyCommentCount = (TextView)convertView.findViewById(R.id.replyCountOfTopicReplyListView);
        Button moreBtn = (Button)convertView.findViewById(R.id.more_btn);


        //设置内容
        //实际的设置头像
        //↓
        //userImageView.setImageResource(topicReply.getResID());
        userImageView.setImageResource(R.drawable.ic_launcher);
        userName.setText(topicReply.getUserName());
        replyContent.setText(topicReply.getReplyContent());
        replyFavourCount.setText("赞同"+String.valueOf(topicReply.getFavoCount()));
        replyCommentCount.setText("踩"+String.valueOf(topicReply.getReplyCount()));


        //设置监听
        moreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMore(v,topicReply);
            }
        });


        return convertView;
    }

    /**
     * 弹出点赞（喜欢）以及评论框
     * @param showMoreBtn
     */
    private void showMore(View showMoreBtn,final TopicReply theTopicReply){
        if(myPopupWindow==null){
            //LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View content = li.inflate(R.layout.topic_more_favour_and_comment,null,false);

            myPopupWindow = new PopupWindow(content,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

            myPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            myPopupWindow.setOutsideTouchable(true);
            myPopupWindow.setTouchable(true);

            content.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            myPopupWindowWidth = content.getMeasuredWidth();
            myPopupWindowHeight = content.getMeasuredHeight();

            View parentView = myPopupWindow.getContentView();

            final TextView favour = (TextView)parentView.findViewById(R.id.likeReply);
            TextView commentReply = (TextView)parentView.findViewById(R.id.commentReply);

            favour.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //设置
                    //图像样子形状改变
                    //待实现

                    theTopicReply.setFavoCount(theTopicReply.getFavoCount()+1);
                    //只能点击一次
                    favour.setEnabled(false);
                }
            });

            commentReply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }//end total IF
        if(myPopupWindow.isShowing()){
            myPopupWindow.dismiss();
        }else{
            //int heighOfMoreBtn = showMoreBtn.getHeight();

            myPopupWindow.showAsDropDown(showMoreBtn,-myPopupWindowWidth,-(myPopupWindowHeight+showMoreBtn.getHeight())/2);
        }
    }//end method showMore
}//end class
